package com.sunny.hp.common.util;

import java.util.Iterator;	
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * 	chart data util
 * @version : 2013. 11. 13.
 * @author : 배민식
 * BEMS2.1 / package com.ncomz.nzeip.common.util;
 */

public class ChartDataUtil {
	/**
	 * function chart data to highchart data
	 * @param List<LinkedHashMap> chartDataList
	 * @return LinkedHashMap
	 */
	public static LinkedHashMap prevChartDataConvert(List<LinkedHashMap> chartDataList){
		LinkedHashMap returnMap = new LinkedHashMap();
		LinkedList<Object> categoryList = new LinkedList<Object>();
		
		for(LinkedHashMap sublhm: chartDataList){
			if(!categoryList.contains(sublhm.get("data_name1"))){
				categoryList.add(sublhm.get("data_name1"));
			}
		}
		
		LinkedHashMap seriesMapList = new LinkedHashMap();
		LinkedList seriesList = new LinkedList();
		
		for(LinkedHashMap prevMap: chartDataList){
			if(null != prevMap.get("series") && !"".equals(prevMap.get("series"))){
				if(!seriesList.contains(prevMap.get("series"))){
					seriesList.add(prevMap.get("series"));
					seriesMapList.put(prevMap.get("series"), new LinkedList());
				}
				
				LinkedList dataList = (LinkedList)seriesMapList.get(prevMap.get("series"));
				dataList.add(prevMap.get("value"));
			}
		}
		returnMap.put("category", categoryList);
		returnMap.put("data", seriesMapList);
		return returnMap;
	}
	/**
	 * 차트 데이터 생성(신규)
	 * @param List<LinkedHashMap> chartDataList
	 * @return LinkedHashMap
	 */
	public static LinkedHashMap genaralDataConvert(List<LinkedHashMap> chartDataList){
		LinkedHashMap rmap = new LinkedHashMap();
		LinkedHashMap returnMap = new LinkedHashMap();
		
		if(null != chartDataList && 0 < chartDataList.size()){
			Iterator it = chartDataList.get(0).keySet().iterator();
			LinkedList<Object> categoryList = new LinkedList<Object>();
			int rCnt = 0;
			while(it.hasNext()){
				String k = (String)it.next();	
				LinkedList<Object> rlist = new LinkedList<Object>();
				for(LinkedHashMap sublhm: chartDataList){
					if(0 == rCnt) {
						// 카테고리 담기(x 축)
						categoryList.add(sublhm.get(k));
					}else{
						rlist.add(sublhm.get(k));
					}
				}
				if(0 < rCnt) {
					rmap.put(k, rlist);
				}
				rCnt++;
			}
			returnMap.put("category", categoryList);
			returnMap.put("data", rmap);
		}
		return returnMap;
	}
	
	/**
	 * ********************************************************************
	 * <PRE> * 분포 차트 convert *
	 * 1. MethodName	: distributionDataConvert
	 * 2. ClassName		: ChartDataUtil
	 * 3. 작성자			: P045701
	 * 4. 작성일			: 2013. 12. 26. 오후 2:42:23
	 * 5. 설명			:
	 * </PRE>
	 * 		@return LinkedHashMap
	 * 		@param chartDataList
	*********************************************************************
	 */
	public static LinkedHashMap distributionDataConvert(List<LinkedHashMap> chartDataList){
		LinkedHashMap returnMap = new LinkedHashMap();
		String data_name1 = "";
		
		if(null != chartDataList && 0 < chartDataList.size()){
			LinkedList dataList = null;
			for(LinkedHashMap data:chartDataList){
				if(!data.get("data_name1").equals(data_name1)){
					dataList = new LinkedList();					
					data_name1 = (String)data.get("data_name1");
					returnMap.put(data.get("data_name1"), dataList);
				}
				LinkedList xydata = new LinkedList();
				xydata.push(data.get("valueY"));
				xydata.push(data.get("valueX"));
				dataList.add(xydata);
			}
		}
		return returnMap;
	}
	
	public static void main(String[] args) throws Exception{
		
		
		LinkedList datalist = new LinkedList();
		LinkedHashMap param1 = new LinkedHashMap();	param1.put("data_name1","냉수펌프2상태값_CHX_24h");	param1.put("valueX","1");	param1.put("valueY","0");	datalist.add(param1);
		LinkedHashMap param2 = new LinkedHashMap();	param2.put("data_name1","냉수펌프2상태값_CHX_24h");	param2.put("valueX","2");	param2.put("valueY","0");	datalist.add(param2);
		
		/*
		LinkedHashMap param1 = new LinkedHashMap();	param1.put("data_name1","방어율");	param1.put("valueX","2");	param1.put("valueY","1");	datalist.add(param1);
		LinkedHashMap param2 = new LinkedHashMap();	param2.put("data_name1","방어율");	param2.put("valueX","4");	param2.put("valueY","3");	datalist.add(param2);
		LinkedHashMap param3 = new LinkedHashMap();	param3.put("data_name1","방어율");	param3.put("valueX","5");	param3.put("valueY","5");	datalist.add(param3);
		LinkedHashMap param4 = new LinkedHashMap();	param4.put("data_name1","방어율");	param4.put("valueX","4");	param4.put("valueY","7");	datalist.add(param4);
		LinkedHashMap param5 = new LinkedHashMap();	param5.put("data_name1","방어율");	param5.put("valueX","2");	param5.put("valueY","9");	datalist.add(param5);
		LinkedHashMap param6 = new LinkedHashMap();	param6.put("data_name1","방어율");	param6.put("valueX","12");	param6.put("valueY","11");	datalist.add(param6);
		LinkedHashMap param7 = new LinkedHashMap();	param7.put("data_name1","방어율");	param7.put("valueX","2");	param7.put("valueY","13");	datalist.add(param7);
		LinkedHashMap param8 = new LinkedHashMap();	param8.put("data_name1","방어율");	param8.put("valueX","5");	param8.put("valueY","15");	datalist.add(param8);
		LinkedHashMap param9 = new LinkedHashMap();	param9.put("data_name1","방어율");	param9.put("valueX","34");	param9.put("valueY","17");	datalist.add(param9);
		LinkedHashMap param10 = new LinkedHashMap();	param10.put("data_name1","방어율");	param10.put("valueX","436");	param10.put("valueY","19");	datalist.add(param10);
		LinkedHashMap param11 = new LinkedHashMap();	param11.put("data_name1","방어율");	param11.put("valueX","12");	param11.put("valueY","21");	datalist.add(param11);
		LinkedHashMap param12 = new LinkedHashMap();	param12.put("data_name1","방어율");	param12.put("valueX","423");	param12.put("valueY","23");	datalist.add(param12);
		LinkedHashMap param13 = new LinkedHashMap();	param13.put("data_name1","흡수율");	param13.put("valueX","23");	param13.put("valueY","25");	datalist.add(param13);
		LinkedHashMap param14 = new LinkedHashMap();	param14.put("data_name1","흡수율");	param14.put("valueX","45");	param14.put("valueY","27");	datalist.add(param14);
		LinkedHashMap param15 = new LinkedHashMap();	param15.put("data_name1","흡수율");	param15.put("valueX","3");	param15.put("valueY","29");	datalist.add(param15);
		LinkedHashMap param16 = new LinkedHashMap();	param16.put("data_name1","흡수율");	param16.put("valueX","3");	param16.put("valueY","31");	datalist.add(param16);
		LinkedHashMap param17 = new LinkedHashMap();	param17.put("data_name1","흡수율");	param17.put("valueX","2");	param17.put("valueY","33");	datalist.add(param17);
		LinkedHashMap param18 = new LinkedHashMap();	param18.put("data_name1","흡수율");	param18.put("valueX","12");	param18.put("valueY","35");	datalist.add(param18);
		LinkedHashMap param19 = new LinkedHashMap();	param19.put("data_name1","흡수율");	param19.put("valueX","23");	param19.put("valueY","37");	datalist.add(param19);
		LinkedHashMap param20 = new LinkedHashMap();	param20.put("data_name1","흡수율");	param20.put("valueX","345");	param20.put("valueY","39");	datalist.add(param20);
		LinkedHashMap param21 = new LinkedHashMap();	param21.put("data_name1","흡수율");	param21.put("valueX","25");	param21.put("valueY","41");	datalist.add(param21);
		LinkedHashMap param22 = new LinkedHashMap();	param22.put("data_name1","잉여율");	param22.put("valueX","4");	param22.put("valueY","43");	datalist.add(param22);
		LinkedHashMap param23 = new LinkedHashMap();	param23.put("data_name1","잉여율");	param23.put("valueX","34");	param23.put("valueY","45");	datalist.add(param23);
		LinkedHashMap param24 = new LinkedHashMap();	param24.put("data_name1","잉여율");	param24.put("valueX","64");	param24.put("valueY","47");	datalist.add(param24);
		LinkedHashMap param25 = new LinkedHashMap();	param25.put("data_name1","잉여율");	param25.put("valueX","3");	param25.put("valueY","49");	datalist.add(param25);
		LinkedHashMap param26 = new LinkedHashMap();	param26.put("data_name1","잉여율");	param26.put("valueX","12");	param26.put("valueY","51");	datalist.add(param26);
		LinkedHashMap param27 = new LinkedHashMap();	param27.put("data_name1","잉여율");	param27.put("valueX","3");	param27.put("valueY","53");	datalist.add(param27);
		LinkedHashMap param28 = new LinkedHashMap();	param28.put("data_name1","잉여율");	param28.put("valueX","32");	param28.put("valueY","55");	datalist.add(param28);
		LinkedHashMap param29 = new LinkedHashMap();	param29.put("data_name1","잉여율");	param29.put("valueX","34");	param29.put("valueY","57");	datalist.add(param29);
		LinkedHashMap param30 = new LinkedHashMap();	param30.put("data_name1","잉여율");	param30.put("valueX","34");	param30.put("valueY","59");	datalist.add(param30);
		LinkedHashMap param31 = new LinkedHashMap();	param31.put("data_name1","잉여율");	param31.put("valueX","6");	param31.put("valueY","61");	datalist.add(param31);
		LinkedHashMap param32 = new LinkedHashMap();	param32.put("data_name1","잉여율");	param32.put("valueX","567");	param32.put("valueY","63");	datalist.add(param32);
		LinkedHashMap param33 = new LinkedHashMap();	param33.put("data_name1","아리가또");	param33.put("valueX","566");	param33.put("valueY","65");	datalist.add(param33);
		LinkedHashMap param34 = new LinkedHashMap();	param34.put("data_name1","아리가또");	param34.put("valueX","7");	param34.put("valueY","67");	datalist.add(param34);
		LinkedHashMap param35 = new LinkedHashMap();	param35.put("data_name1","아리가또");	param35.put("valueX","5656");	param35.put("valueY","69");	datalist.add(param35);
		LinkedHashMap param36 = new LinkedHashMap();	param36.put("data_name1","아리가또");	param36.put("valueX","7");	param36.put("valueY","71");	datalist.add(param36);
		LinkedHashMap param37 = new LinkedHashMap();	param37.put("data_name1","아리가또");	param37.put("valueX","34");	param37.put("valueY","73");	datalist.add(param37);
		LinkedHashMap param38 = new LinkedHashMap();	param38.put("data_name1","아리가또");	param38.put("valueX","345");	param38.put("valueY","75");	datalist.add(param38);
		LinkedHashMap param39 = new LinkedHashMap();	param39.put("data_name1","아리가또");	param39.put("valueX","3");	param39.put("valueY","77");	datalist.add(param39);
		LinkedHashMap param40 = new LinkedHashMap();	param40.put("data_name1","아리가또");	param40.put("valueX","45");	param40.put("valueY","79");	datalist.add(param40);
		LinkedHashMap param41 = new LinkedHashMap();	param41.put("data_name1","아리가또");	param41.put("valueX","34");	param41.put("valueY","81");	datalist.add(param41);
		LinkedHashMap param42 = new LinkedHashMap();	param42.put("data_name1","아리가또");	param42.put("valueX","345");	param42.put("valueY","83");	datalist.add(param42);
		LinkedHashMap param43 = new LinkedHashMap();	param43.put("data_name1","아리가또");	param43.put("valueX","5");	param43.put("valueY","85");	datalist.add(param43);
*/
		//System.out.println(distributionDataConvert(datalist));
	}
}
